import { Categorys } from './category';
let _Categorys = Categorys;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import CategoryMock from './mallm/pub/categoryMock';
 * CategoryMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/pub/category/listKey').reply(config => {
      let params = config.params;
      let mockCategorys = _Categorys.filter(category => {
      	if(params.id||params.id==''){ if( category.id.indexOf(params.id)>=0) return true;}
      	if(params.name||params.name==''){ if( category.name.indexOf(params.name)>=0) return true;}
      	if(params.keywords||params.keywords==''){ if( category.keywords.indexOf(params.keywords)>=0) return true;}
      	if(params.frontDesc||params.frontDesc==''){ if( category.frontDesc.indexOf(params.frontDesc)>=0) return true;}
      	if(params.parentId||params.parentId==''){ if( category.parentId.indexOf(params.parentId)>=0) return true;}
      	if(params.sortOrder||params.sortOrder==''){ if( category.sortOrder.indexOf(params.sortOrder)>=0) return true;}
      	if(params.showIndex||params.showIndex==''){ if( category.showIndex.indexOf(params.showIndex)>=0) return true;}
      	if(params.isShow||params.isShow==''){ if( category.isShow.indexOf(params.isShow)>=0) return true;}
      	if(params.bannerUrl||params.bannerUrl==''){ if( category.bannerUrl.indexOf(params.bannerUrl)>=0) return true;}
      	if(params.iconUrl||params.iconUrl==''){ if( category.iconUrl.indexOf(params.iconUrl)>=0) return true;}
      	if(params.imgUrl||params.imgUrl==''){ if( category.imgUrl.indexOf(params.imgUrl)>=0) return true;}
      	if(params.wapBannerUrl||params.wapBannerUrl==''){ if( category.wapBannerUrl.indexOf(params.wapBannerUrl)>=0) return true;}
      	if(params.levels||params.levels==''){ if( category.levels.indexOf(params.levels)>=0) return true;}
      	if(params.type||params.type==''){ if( category.type.indexOf(params.type)>=0) return true;}
      	if(params.frontName||params.frontName==''){ if( category.frontName.indexOf(params.frontName)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( category.deptid.indexOf(params.deptid)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockCategorys
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/pub/category/list').reply(config => {
      let params = config.params;
      let mockCategorys = _Categorys.filter(category => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(category.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.name||params.name==''){if(category.name.indexOf(params.name)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.keywords||params.keywords==''){if(category.keywords.indexOf(params.keywords)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.frontDesc||params.frontDesc==''){if(category.frontDesc.indexOf(params.frontDesc)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.parentId||params.parentId==''){if(category.parentId.indexOf(params.parentId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.sortOrder||params.sortOrder==''){if(category.sortOrder.indexOf(params.sortOrder)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.showIndex||params.showIndex==''){if(category.showIndex.indexOf(params.showIndex)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isShow||params.isShow==''){if(category.isShow.indexOf(params.isShow)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.bannerUrl||params.bannerUrl==''){if(category.bannerUrl.indexOf(params.bannerUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.iconUrl||params.iconUrl==''){if(category.iconUrl.indexOf(params.iconUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.imgUrl||params.imgUrl==''){if(category.imgUrl.indexOf(params.imgUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.wapBannerUrl||params.wapBannerUrl==''){if(category.wapBannerUrl.indexOf(params.wapBannerUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.levels||params.levels==''){if(category.levels.indexOf(params.levels)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.type||params.type==''){if(category.type.indexOf(params.type)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.frontName||params.frontName==''){if(category.frontName.indexOf(params.frontName)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(category.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockCategorys
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockCategorys.length;
      mockCategorys = mockCategorys.filter((category, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockCategorys,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/pub/category/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _Categorys = _Categorys.filter(category => category.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/pub/category/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _Categorys = _Categorys.filter(category => !params.includes(category.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/pub/category/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,name,keywords,frontDesc,parentId,sortOrder,showIndex,isShow,bannerUrl,iconUrl,imgUrl,wapBannerUrl,levels,type,frontName,deptid } = params;
      _Categorys.some(category => {
        if (category.id === id) {
			category.id = id;
			category.name = name;
			category.keywords = keywords;
			category.frontDesc = frontDesc;
			category.parentId = parentId;
			category.sortOrder = sortOrder;
			category.showIndex = showIndex;
			category.isShow = isShow;
			category.bannerUrl = bannerUrl;
			category.iconUrl = iconUrl;
			category.imgUrl = imgUrl;
			category.wapBannerUrl = wapBannerUrl;
			category.levels = levels;
			category.type = type;
			category.frontName = frontName;
			category.deptid = deptid
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/pub/category/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,name,keywords,frontDesc,parentId,sortOrder,showIndex,isShow,bannerUrl,iconUrl,imgUrl,wapBannerUrl,levels,type,frontName,deptid } = params;
      _Categorys.push({
		id:id,
		name:name,
		keywords:keywords,
		frontDesc:frontDesc,
		parentId:parentId,
		sortOrder:sortOrder,
		showIndex:showIndex,
		isShow:isShow,
		bannerUrl:bannerUrl,
		iconUrl:iconUrl,
		imgUrl:imgUrl,
		wapBannerUrl:wapBannerUrl,
		levels:levels,
		type:type,
		frontName:frontName,
		deptid:deptid
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};